﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using BitPortal.Models;
using BitPortal.Business;
using BitPortal.Utils;
using System.Web.Security;

namespace BitPortal.Admin.Note.Module
{
    public partial class NoteAddEdit : Controls.BaseAdminControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            txtDefaultRoleView.Text = "Customer#Employee#Admin#";
            int flag = Membership.GetUser().GetHighestRole();
            if (flag >= 0)
            {
                cbRoles.Items.Add(new Ext.Net.ListItem("Mọi khách hàng", "Customer"));
                txtDefaultRoleView.Text = txtDefaultRoleView.Text.Replace("Customer#", "");
            }
            if (flag >= 1)
            {
                cbRoles.Items.Add(new Ext.Net.ListItem("Mọi nhân viên", "Employee"));
                txtDefaultRoleView.Text = txtDefaultRoleView.Text.Replace("Employee#", "");
            }
            if (flag >= 2)
            {
                cbRoles.Items.Add(new Ext.Net.ListItem("Người quản trị", "Admin"));
                txtDefaultRoleView.Text = txtDefaultRoleView.Text.Replace("Admin#", "");
            }
        }
        protected void btnInsert_Click(object sender, DirectEventArgs e)
        {
            try
            {
                Models.Note n = new Models.Note();
                n.Subject = txtSubject.Text;
                n.Content = txtContent.Text;
                n.SortOrder = txtSortOrder.Text.ToInt64TryParse();
                n.IsEnabled = ckIsEnabled.Checked;
                n.Attachs = "";
                if (!string.IsNullOrEmpty(AttachFile1.TextFieldURL.Text)) n.Attachs += AttachFile1.TextFieldURL.Text + "#";
                if (!string.IsNullOrEmpty(AttachFile2.TextFieldURL.Text)) n.Attachs += AttachFile2.TextFieldURL.Text + "#";
                if (!string.IsNullOrEmpty(AttachFile3.TextFieldURL.Text)) n.Attachs += AttachFile3.TextFieldURL.Text + "#";
                //NoteGroupID
                long groupID = Utils.GeneralUtils.ToInt64TryParse(Request.QueryString["NoteGroupID"]);
                if (groupID <= 0) return;
                n.NoteGroupID = groupID;
                //Roles View
                n.AllowRolesView = "#";
                if (cbRoles.SelectedItems != null)
                {
                    foreach (var item in cbRoles.SelectedItems)
                    {
                        n.AllowRolesView += item.Value + "#";
                    }
                }
                n.AllowRolesView += txtDefaultRoleView.Text;
                //Logs
                n.Logs.DatetimeCreated = n.Logs.DatetimeEdited = DateTime.Now;
                n.Logs.CreatingUser = n.Logs.EditingUser = BitContext.Current.LoginUser;

                if (BitPortal.Business.NoteBLL.Insert(n) > 0)
                {
                    (this.Page as Controls.BaseAdminPage).ShowNotification("Thêm ghi chú thành công!");
                    Window1.Hide();
                    ReloadTargetData();
                }
            }
            catch (Exception ex)
            {
                (this.Page as Controls.BaseAdminPage).ShowMessage(ex.Message);
            }
        }

        protected void btnUpdate_Click(object sender, DirectEventArgs e)
        {
            try
            {
                ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
                long noteID = txtID.Text.ToInt64TryParse();
                BitPortal.Models.Note n = bitEnt.Notes.SingleOrDefault(ent => ent.NoteID == noteID);

                if (n != null)
                {
                    if (!n.CheckPermissonToEdit()) throw new Exception("Bạn không có quyền sửa ghi chú này!");

                    n.Subject = txtSubject.Text;
                    n.Content = txtContent.Text;
                    n.IsEnabled = ckIsEnabled.Checked;
                    n.SortOrder = txtSortOrder.Text.ToInt64TryParse();
                    n.Attachs = "";
                    if (!string.IsNullOrEmpty(AttachFile1.TextFieldURL.Text)) n.Attachs += AttachFile1.TextFieldURL.Text + "#";
                    if (!string.IsNullOrEmpty(AttachFile2.TextFieldURL.Text)) n.Attachs += AttachFile2.TextFieldURL.Text + "#";
                    if (!string.IsNullOrEmpty(AttachFile3.TextFieldURL.Text)) n.Attachs += AttachFile3.TextFieldURL.Text + "#";

                    //Roles View
                    n.AllowRolesView = "#";
                    if (cbRoles.SelectedItems != null)
                    {
                        foreach (var item in cbRoles.SelectedItems)
                        {
                            n.AllowRolesView += item.Value + "#";
                        }
                    }
                    n.AllowRolesView += txtDefaultRoleView.Text;
                    //Logs
                    n.Logs.DatetimeEdited = DateTime.Now;
                    n.Logs.EditingUser = BitContext.Current.LoginUser;
                }
                if (bitEnt.SaveChanges() > 0)
                {
                    (this.Page as Controls.BaseAdminPage).ShowNotification("Sửa ghi chú thành công!");
                    Window1.Hide();
                    ReloadTargetData();
                }
            }
            catch (Exception ex)
            {
                (this.Page as Controls.BaseAdminPage).ShowMessage(ex.Message);
            }
        }

        public void BindData(long noteID = 0)
        {
            try
            {
                if (noteID == 0)
                {
                    txtID.Text = txtSortOrder.Text = "0";
                    txtSubject.Text = txtContent.Text =
                    AttachFile1.TextFieldURL.Text =
                    AttachFile2.TextFieldURL.Text =
                    AttachFile3.TextFieldURL.Text = "";
                    ckIsEnabled.Checked = true;

                    btnUpdate.Hidden = true;
                    btnInsert.Hidden = false;
                    Window1.Show();
                    return;
                }
                Models.Note n = Business.NoteBLL.GetByID(noteID);
                if (n != null)
                {
                    if (!n.CheckPermissonToEdit()) throw new Exception("Bạn không có quyền sửa ghi chú này!");

                    txtID.Text = n.NoteID.ToString();
                    txtSubject.Text = n.Subject;
                    txtContent.Text = n.Content;
                    txtSortOrder.Text = n.SortOrder.ToString();
                    ckIsEnabled.Checked = n.IsEnabled;
                    cbRoles.SelectedItems.Clear();
                    if (!string.IsNullOrEmpty(n.AllowRolesView))
                    {
                        foreach (string str in n.AllowRolesView.Split('#').Where(ent => !string.IsNullOrEmpty(ent)))
                        {
                            cbRoles.SelectedItems.Add(new SelectedListItem(str));
                        }
                    }
                    cbRoles.UpdateSelection();
                    //Attachs
                    var attachs = n.Attachs.Split('#').ToList().Where(ent => !string.IsNullOrEmpty(ent)).ToList();
                    if (attachs.Count >= 1) AttachFile1.TextFieldURL.Text = attachs[0];
                    if (attachs.Count >= 2) AttachFile2.TextFieldURL.Text = attachs[1];
                    if (attachs.Count >= 3) AttachFile3.TextFieldURL.Text = attachs[2];
                    //
                    btnUpdate.Hidden = false;
                    btnInsert.Hidden = true;
                    Window1.Show();
                }
            }
            catch (Exception ex)
            {
                (this.Page as Controls.BaseAdminPage).ShowMessage(ex.Message);
            }
        }


    }
}